package com.bdqn.servlet.emp;


import com.bdqn.entity.Calendar.Event;
import com.bdqn.entity.Calendar.Record;
import com.bdqn.entity.Employee;
import com.bdqn.service.EmployeeService;
import com.bdqn.service.Impl.EmployeeServiceImpl;
import com.bdqn.util.Converter;
import com.bdqn.util.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "CalendarServlet",urlPatterns = "/calendar")
public class CalendarServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Employee emp = (Employee)request.getSession().getAttribute("emp");
        String startstr = request.getParameter("begin");
        String endstr = request.getParameter("end");
        String end = endstr.substring(0,10);
        String start = startstr.substring(0,10);
        EmployeeService service = new EmployeeServiceImpl();
        List<Record> records = service.findRecord(emp.getEmpId(),start,end);
        List<Event> events = Converter.toEventList(records);
        JSON.writeAsString(events,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
